<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'list','action'=>'/manage.article_fenlei/','title'=>'文章分类'),
	array('op'=>'edit','action'=>'/manage.article_fenlei/','title'=>'添加文章分类'),
	array('op'=>'list','action'=>'/manage.article_list/','title'=>'返回文章列表'),
);
if($_RQ['op']=='list'){
	//搜索
	$params=array('uniacid'=>$_SESSION['uniacid'],'plugin_name'=>'');
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$pindex = max(1, intval($_RQ['page']));
	if($_RQ['num']){
		$psize = $_RQ['num'];
	}else{
		$psize = 15;
	}
	$total='';
	$parameter=array(
		'params'=>$params,
		'fields'=>$fields,
		'limit'=>array($pindex,$psize),
	);
	$list=pdo_getslice('core_account_article_fenlei',$params,array($pindex,$psize),$total,$fields,'','paixu desc,id desc');
	if($list){
		foreach($list as &$item){
			$item['url']=custom_url('core/article.list//group_id='.$item['id'],true);
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'paixu','title'=>'排序(点击编辑↓)','type'=>'string','set'=>'set'),
			array('name'=>'picurl','title'=>'分类图标','type'=>'image'),
			array('name'=>'title','title'=>'分类标题','type'=>'string'),
			array('name'=>'url','title'=>'二维码','type'=>'ewm'),
			array('name'=>'url','title'=>'复制链接','type'=>'copy'),
		)),
		'search_one'=>array('name'=>'title'),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择文章！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['paixu'])){
		$update['paixu']=$post['paixu'];
	}
	if($update){
		pdo_update('core_account_article_fenlei',$update,array('id'=>$id));
	}
	exi('操作成功！');
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('core_account_article_fenlei',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'分类',
		'tabs'=>$tabs,
		'edit'=>array(
			array('type'=>'string','title'=>'标题','name'=>'title', 'help'=>'' ),
			array('type'=>'string','title'=>'描述','name'=>'description', 'help'=>'' ),
			array('type'=>'image','title'=>'图标','name'=>'picurl', 'help'=>'' ),
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'paixu'=>intval($postdata['paixu']),
		'title'=>trim($postdata['title']),
		'description'=>trim($postdata['description']),
		'picurl'=>trim($postdata['picurl'])
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('core_account_article_fenlei',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('文章分类修改成功！','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('core_account_article_fenlei',$data);
		exi('文章分类增加成功！','','list');
	}
}

if($_RQ['op'] == 'del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('core_account_article_fenlei',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('core_account_article_fenlei',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}

?>